
var webpack = require('webpack');
var MiniCssExtractPlugin = require("mini-css-extract-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');

//获取 html-webpack-plugin参数的方法
var getHtmlConfig = function(name){
    return {
        template : __dirname + '/src/view/' + name + '.html',
        filename : 'view/' + name + '.html',
        inject : true,
        hash : true,
        chunks : ['common',name]
    };
};


var config = {
    entry:{
        'common' :  [__dirname + '/src/page/common/index.js'],
        'login' :  [__dirname + '/src/page/login/index.js'],
        'logout' :  [__dirname + '/src/page/logout/index.js'],
    },
    output:{
        path: __dirname +'/dist',
        filename: 'js/[name].js'
    },
    plugins:[
        //css 打包
        new MiniCssExtractPlugin(
            {
                filename : 'css/[name].css',
            }
        ),
        new HtmlWebpackPlugin(getHtmlConfig('login')),
        new HtmlWebpackPlugin(getHtmlConfig('logout')),
    ],
    resolve :{
        alias : {
            util : __dirname + '/src/util',
            page : __dirname + '/src/page',
            service : __dirname + '/src/service',
            image : __dirname + '/src/image',
        }
    },
    module : {
        rules : 
        [
            { 
                test : /\.css$/,
                loader : [MiniCssExtractPlugin.loader,"css-loader"]
            },
            { 
                test : /\.(gif|png|jpg|woff|svg|eotttf)\??.*$/,
                // loader : 'url-loader&limit=100&name=resource/[name].[ext]'
                use : 
                {  
                    loader :'url-loader',
                    options:
                    {
                        limit:100,
                        name: '[name].[ext]',
                        outputPath : 'resource/',
                        publicPath : '../resource/'
                    
                    }
                }
            }
                
        ],
    },
   //通用 js 打包
    optimization : {
        splitChunks:{
            cacheGroups: {
                default: {
                    minChunks: 2,
                    priority: -20,
                    reuseExistingChunk: true,
                },
                //打包第三方类库
                vendor: {
                    name: 'vendor',
                    chunks: 'initial',
                    test: /[\\/]node_modules[\\/]/,
                    minSize: 0, 
                    minChunks: 1,

                },
                //打包重复出现的代码
                base: {
                    name: "base",
                    chunks: "initial",
                    test: /[\\/]src[\\/]/,
                    minSize: 0,
                    filename : 'js/base.js',
                    minChunks: 2,
                }
            }
        }
    }

};


module.exports = config;